﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.Widget
{
    /// <summary>
    /// UCLiquidAct.xaml 的交互逻辑
    /// </summary>
    public partial class UCLiquidAct : UserControl
    {
        bool bIsStart = false;
        List<Image> lstWheel = new List<Image>();
        public UCLiquidAct()
        {
            InitializeComponent();

            lstWheel.Add(pump1);
            lstWheel.Add(pump2);
            lstWheel.Add(pump3);
        }

        int nPumpWheelIndex = 0;
        public void AnimationTask()
        {
            bIsStart = true;
            nPumpWheelIndex = 0;
            Task.Run(() =>
            {
                while (bIsStart)
                {
                    Thread.Sleep(1000);
                    this.Dispatcher.Invoke(new Action(
                        delegate
                        {
                            try
                            {
                                for (int i = 0; i < lstWheel.Count; i++)
                                {
                                    if (nPumpWheelIndex == i)
                                        lstWheel[i].Visibility = Visibility.Visible;
                                    else
                                        lstWheel[i].Visibility = Visibility.Collapsed;
                                }

                                nPumpWheelIndex++;

                                if (nPumpWheelIndex >= lstWheel.Count)
                                    nPumpWheelIndex = 0;


                            }
                            catch (Exception)
                            {

                            }
                        }
                        )
                    );


                }
            });
        }

        public void Start()
        {
            if (!bIsStart)
                AnimationTask();
        }

        public void Stop()
        {
            if (bIsStart)
            {
                bIsStart = false;
                Thread.Sleep(1000);

                this.Dispatcher.Invoke(new Action(
                       delegate
                       {
                           try
                           {
                               for (int i = 0; i < lstWheel.Count; i++)
                               {
                                   lstWheel[i].Visibility = Visibility.Collapsed;
                               }
                           }
                           catch (Exception)
                           {

                           }
                       }
                       )
                   );


            }
        }
    }
}
